<?php

namespace App\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */

use App\Controllers\FrontBase;

class UserBase extends FrontBase
{

    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    protected $helpers = [];
    protected $userCongfig;

    /**
     * Constructor.
     */
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        $this->_checkUserLogin();
    }

    //是否登录
    private function _checkUserLogin()
    {
        $this->userid = $this->userModel->getUidBySession();
        $this->user = $this->userModel->getUser($this->userid);

        if (!$this->userid) {
            $this->msg(1, '请登录', ['url' => url('user/login')]);
        }
    }

    //提示消息
    public function userMsg($code, $msg, $data = [])
    {
        $url = isset($data['url']) ? $data['url'] : ''; //跳转的地址
        $time = isset($data['time']) ? intval($data['time']) : 3; //默认3秒跳转

        if (isset($data['url']) && $data['url']) {
            $backurl = $this->xss_clean($data['url']);
            if (strpos($backurl, '?backurl=') !== false) {
                $arr = explode('?backurl=', $backurl);
                if (isset($arr[1]) && $arr[1]) {
                    $backurl = $arr[0] . '?backurl=' . urlencode($arr[1]);
                }
            }
        } else if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'php?') !== false) {
            $backurl = $this->xss_clean($_SERVER['HTTP_REFERER']);
        } else if (!$backurl) {
            $backurl = SELF;
        }

        $this->request->getGet('callback') && exit($this->_jsonp($code, $msg));
        if ($this->request->getGet('is_ajax') || IS_AJAX || $this->request->isAJAX()) {
            exit($this->_json($code, $msg));
        }

        $vdata = [
            'code' => $code,
            'msg' => $msg,
            'url' => $backurl,
            'time' => $time,
            'mark' => $code,
        ];
        echo view('user/msg.html', $vdata);
        exit();
    }

    //添加
    public function addOauthUser($data)
    {
        if (!$data['openid']) {
            return ams_rt(1, '参数错误');
        }
        $row = $this->db->table($this->tablename . '_oauth')->where('openid', $data['openid'])->get()->getRowArray();
        if ($row) {
            $this->db->table($this->tablename . '_oauth')->where('openid', $data['openid'])->update();
            return ams_rt(0, 'ok', $data);
        } else {
            $this->db->table($this->tablename . '_oauth')->insert($data);
            $id = $this->db->insertID();
            if ($id) {
                return ams_rt(0, 'ok', $data);
            }
        }
        return ams_rt(1, '操作失败');
    }

    //初始化检查
    public function init()
    {
        $user = $this->user;
        $config = $this->userModel->getConfig();
        if ($config['regverify'] == 1 && $user['status'] == 2) {
            $this->userMsg(1, '请等待管理员审核', ['url' => '/']);
        }

        if ($user['status'] == 0 || !$user['status']) {
            $this->userMsg(1, '账号可能已被禁用', ['url' => '/']);
        }

        //完善资料
        if ($config['complete'] == 1 && !$user['iscomplete'] && ($this->uripath != 'user/profile/complete')) {
            $this->userMsg(1, '请完善资料', ['url' => url('user/profile/complete')]);
        }

        //认证相关
        if ($config['ismobile'] == 1 && !$user['iscomplete'] && ($this->uripath != 'user/profile/mobile')) {
            $this->userMsg(1, '请完成手机认证证', ['url' => url('user/profile/complete')]);
        }

        //
        if ($config['isemail'] == 1 && !$user['iscomplete'] && ($this->uripath != 'user/profile/email')) {
            $this->userMsg(1, '请完成邮箱认证', ['url' => url('user/profile/complete')]);
        }

        if ($config['istruename'] == 1 && !$user['iscomplete'] && ($this->uripath != 'user/profile/truename')) {
            $this->userMsg(1, '请完成实名认证', ['url' => url('user/profile/complete')]);
        }

    }

}
